﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickApp.MVCControl;
using System.Web.Script.Serialization;

namespace System.Web.Mvc.Html
{
    /// <summary>
    /// HtmlHelper扩展类
    /// </summary>
    public static class HtmlHelperExtension
    {
        /// <summary>
        /// 输出样式表到前台UI界面，样式表必须放在主题文件夹下，如： ./Themes/主题文件夹/ 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="theme">主题样式</param>
        /// <param name="cssfilename">样式表文件名称</param>
        /// <returns></returns>
        public static MvcHtmlString Css(this HtmlHelper html, Theme theme, params string[] cssfilename)
        {
            if (cssfilename != null)
            {
                string folderpath = "~/Content/themes/" + theme;
                string csslink = "<link href=\"{0}\" rel=\"Stylesheet\" type=\"text/css\" />";
                StringBuilder sb = new StringBuilder();
                foreach (string filename in cssfilename)
                {
                    sb.AppendFormat(csslink, UrlHelper.GenerateContentUrl(folderpath + "/" + filename + ".css", html.ViewContext.HttpContext));
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            return MvcHtmlString.Empty;
        }

        /// <summary>
        /// 输出样式表到前台UI界面，样式表必须放在主题文件夹下，如： ./Themes/主题文件夹/ 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="cssfilename">样式表文件名称</param>
        /// <returns></returns>
        public static MvcHtmlString Css(this HtmlHelper html, params string[] cssfilename)
        {
            return html.Css(Theme.Blue, cssfilename);
        }

        /// <summary>
        /// 输出Js脚本到前台UI界面，Js脚本必须放在Scripts文件夹下
        /// </summary>
        /// <param name="html"></param>
        /// <param name="jsKeys">Js脚本文件名称</param>
        /// <returns></returns>
        public static MvcHtmlString Js(this HtmlHelper html, params string[] jsKeys)
        {
            if (jsKeys != null)
            {
                string folderpath = "~/Scripts/";
                string jslink = "<script src='{0}' type='text/javascript'></script>";
                StringBuilder sb = new StringBuilder();
                foreach (string jsname in jsKeys)
                {
                    sb.AppendFormat(jslink, UrlHelper.GenerateContentUrl(folderpath + "/" + jsname + ".js", html.ViewContext.HttpContext));
                }
                return MvcHtmlString.Create(sb.ToString());
            }
            return MvcHtmlString.Empty;

        }

        public static string GridLink(this UrlHelper url, string btnClass, string title, string jsEvent, object htmlAttributes = null)
        {
            TagBuilder tagBuilder = new TagBuilder("a");
            tagBuilder.AddCssClass("imgbtn2");
            tagBuilder.MergeAttributes<string, object>(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            tagBuilder.MergeAttribute("href", string.Format("javascript:{0}", jsEvent));
            tagBuilder.InnerHtml = string.Format("<span class=\"{0}\">{1}</span></a>&nbsp;", btnClass, title);

            return tagBuilder.ToString();
        }

        public static string EditLink(this UrlHelper url, string actionName, string title, object routeValues = null)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a class=\"imgbtn2\" href=\"javascript:OpenModelWindow('{0}", url.Action(actionName, routeValues));
            sb.Append("/${Id}',{ width: 470, height: 330, caption: '" + title + "', onclose: refreshGrid })\">");
            sb.Append("<span class=\"edit\">编辑</span></a>&nbsp;");
            return sb.ToString();
        }

        public static string EditLinkTwo(this UrlHelper url, string actionUrl, string title, object options = null)
        {
            StringBuilder sb = new StringBuilder();
            var editUrl = actionUrl + (actionUrl.IndexOf("?") >= 0 ? "&id=${Id}" : "?id=${Id}");
            sb.AppendFormat("<a class=\"imgbtn2\" href=\"javascript:OpenFormDialog('{0}','{1}'", editUrl, title);
            if (options != null)
            {
                sb.AppendFormat(",{0}", new JavaScriptSerializer().Serialize(options));
            }
            sb.Append(")\">");
            sb.Append("<span class=\"edit\">编辑</span></a>&nbsp;");
            return sb.ToString();
        }

        public static string DeleteLink(this UrlHelper url, string actionName, string name, object routeValues = null)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a class=\"imgbtn2\" href=\"javascript:DeleteRecord('{0}", url.Action(actionName, routeValues));
            sb.Append("/${Id}','" + name + "')\">");
            sb.Append("<span class=\"delete\">删除</span></a>&nbsp;");
            return sb.ToString();
        }
    }
}
